package ks.client.controllers.commands;

import org.w3c.dom.Document;

import ks.client.kommander.Action;
import ks.client.lobby.Lobby;
import ks.framework.common.Message;

public class OptionsCommandController {
	private Lobby lobby;
	
	public OptionsCommandController(Lobby l) {
		this.lobby = l;
	}
	
	/**
	 * Just send the options command.
	 * 
	 * @param tableNum
	 * @param time
	 * @param pluginName
	 * @param undo
	 * @param newhand
	 * @param seed
	 * @param type
	 */
	public void send(int tableNum, int time, String pluginName, boolean undo, boolean newhand, int seed, String type) {
		String uid = lobby.getKommander().generateUid();
		
		StringBuilder sb = new StringBuilder("<command version=\"1.0\" id=\"" + uid + "\">");
		sb.append("<options table=\"" + tableNum + "\" time=\"" + time + "\" plugin=\"" + pluginName + "\" undo=\"" + undo + "\" newhand=\"" + newhand + "\" seed=\"" + seed + "\" type=\"" + type + "\"/>");
		sb.append("</command>");
		
		Document d = Message.construct(sb.toString());
		Message m = new Message(d);
		lobby.getUserContext().getClient().sendToServer(m);
	}
	
	/**
	 * Send the options command and perform an Action on ack/nack.
	 * 
	 * @param tableNum
	 * @param time
	 * @param pluginName
	 * @param undo
	 * @param newhand
	 * @param seed
	 * @param type
	 * @param action
	 */
	public void send(int tableNum, int time, String pluginName, boolean undo, boolean newhand, int seed, String type, Action action) {
		String uid = lobby.getKommander().generateUid();
		
		StringBuilder sb = new StringBuilder("<command version=\"1.0\" id=\"" + uid + "\">");
		sb.append("<options table=\"" + tableNum + "\" time=\"" + time + "\" plugin=\"" + pluginName + "\" undo=\"" + undo + "\" newhand=\"" + newhand + "\" seed=\"" + seed + "\" type=\"" + type + "\"/>");
		sb.append("</command>");
		
		lobby.getKommander().addAction(uid, action);
		
		Document d = Message.construct(sb.toString());
		Message m = new Message(d);
		lobby.getUserContext().getClient().sendToServer(m);
	}
}